package DB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import entity.Product;

public class ProductDao {

	public boolean addById(String productname,int photoId) {
		Connection con = ConnectDBC.getConnection();
		String sql = "insert into  product(productname,photoId) values(?,?)";
		try {
			PreparedStatement pre = con.prepareStatement(sql);
			pre.setString(1, productname);
			pre.setInt(2, photoId);
			int count = pre.executeUpdate();
			pre.close();
			return count > 0 ? true : false;
		} catch (Exception e) {
			System.out.println(e);
			return false;
		}

	}

	public ArrayList<Product> select() {
		Connection con = ConnectDBC.getConnection();
		String sql = "select * from  product";
		ArrayList<Product> arr = new ArrayList<Product>();
		try {

			Statement sta = con.createStatement();
			ResultSet rs = sta.executeQuery(sql);
			while (rs.next()) {
				Product pon = new Product();
				pon.setProductname(rs.getString(1));
				pon.setPhotoId(rs.getInt(2));
				arr.add(pon);
			}
			rs.close();
			sta.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return arr;

	}
	public ArrayList<Product> selectByProductName(String productname){
		Connection con=ConnectDBC.getConnection();
		String sql="select * from product where productname=?";
		ArrayList<Product> arr = new ArrayList<Product>();
	    try {
	    	
	         PreparedStatement pre=con.prepareStatement(sql);
		     pre.setString(1, productname);
		     ResultSet rs=pre.executeQuery();
		while (rs.next()) {
			Product pon = new Product();
			pon.setProductname(rs.getString(1));
			pon.setPhotoId(rs.getInt(2));
			arr.add(pon);
		}
		   rs.close();
	        pre.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arr;
		
	}
}

